package com.caigou.modules.data.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.data.bean.ALlYearDetailBean
import com.caigou.modules.data.bean.AllYearItemBean
import com.caigou.modules.data.bean.DataDetailBean
import com.caigou.modules.data.repository.DataRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/29
 */
@HiltViewModel
class AllYearViewModel @Inject constructor(private val repository: DataRepository) : ViewModel() {

    val allYearListLiveData = MutableLiveData<Resource<MutableList<AllYearItemBean>>>()
    val allYearDetailLiveData = MutableLiveData<Resource<ALlYearDetailBean>>()
    val commentListLiveData = MutableLiveData<Resource<CommentBean>>()

    fun getAllYearList() {
        allYearListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getAllYearList()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<AllYearItemBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    allYearListLiveData.value = it
                }
        }
    }

    fun getAllYearDetail(mysteryId: Int, userId: String) {
        allYearDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getAllYearDetail(mysteryId, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<DataDetailBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    allYearDetailLiveData.value = it
                }
        }
    }

    fun getCommentList(page: Int, limit: Int, formulaId: Int, sort: Int, userId: String) {
        commentListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCommentList(page, limit, formulaId, 2, sort, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CommentBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentListLiveData.value = it
                }
        }
    }
}